package cn.itsource.dept.controller;

import cn.itsource.dept.domain.Department;
import cn.itsource.dept.domain.EmployeeVo;
import cn.itsource.dept.service.IEmployeeService;
import cn.itsource.dept.domain.Employee;
import cn.itsource.dept.query.EmployeeQuery;

import cn.itsource.utills.AjaxResult;
import cn.itsource.utills.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    public IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param employee  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if( employee.getId()!=null)
                employeeService.update(employee);
            else
                employeeService.add(employee);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
                employeeService.delete(id);
            return  AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setSuccess(false).setMsg("操作失败");
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Employee findnoe = employeeService.findnoe(id);
            return AjaxResult.success().setData(findnoe);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Employee> list(){

        return employeeService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/pagelist")
    public AjaxResult findQuey(@RequestBody EmployeeQuery query)
    {
        try {
            List<Employee> queryData = employeeService.queryData(query);
            Long count = employeeService.queryCount(query);
            return AjaxResult.success().setData(queryData).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
    /*导出*/
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Employee> all = employeeService.findAll();
        ExcelUtil.exportExcel(all,"员工数据title","员工数据",Employee.class,"导出的员工数据.xls",response);
    }
    /*导入*/
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Employee> shops = ExcelUtil.importExcel(file, 1, 1, Employee.class);
        //做数据校验
        shops.forEach(System.out::println);
    }
    @GetMapping("/echarts")
    public List<Integer> echartsData(){
        List<EmployeeVo> employeeVos=employeeService.countByDept();
        //把其中的数据转换为list或者数组  前端用于图表的y轴数据
        List<Integer> counts = employeeVos.stream().map(EmployeeVo::getCountNum).collect(Collectors.toList());
        List<String> depts = employeeVos.stream().map(EmployeeVo::getDname).collect(Collectors.toList());
        System.out.println(depts);
        System.out.println(counts);
        return counts;
    }
}
